<?php

session_start();
ob_end_clean();		
ob_start("callback");  	
if (!isset($_SESSION['uid']))
{
	header('Location:index.php?redirection=' . urlencode( $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] ) );
	exit;
}
include_once('config.php');

if (!isset($id) || $id == '')
{
	header('Location:error.php?ec=2');
	exit;
}

$filedata = new FileData($GLOBALS['connection'], $GLOBALS['database'], 'data');
$filedata->setId($id);

if ($filedata->getError() != '')
{
	header('Location:error.php?ec=2');
	ob_end_flush();		
	ob_end_clean();		
	exit;
}
else
{
	
	if (!isset($submit))
	{
		draw_header();
		draw_menu($_SESSION['uid']);
		draw_status_bar('Add New User', $message);
		?>
			<p>

			<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			<input type="hidden" name="id" value="<?php echo $_POST['id']; ?>">
			<input type="submit" name="submit" value="Click here"> to begin downloading the selected document to your local workstation.
			</form>
			Once the document has completed downloading, you may <a href="out.php">continue browsing</a>.
			<?php	

			draw_footer();

    }
   
    else
    {
        $id = $filedata->getId();
        $realname = $filedata->getName();
        

        
        $filename = $GLOBALS['CONFIG']['dataDir'] . $_POST['id'] . '.dat';

        if ( file_exists($filename) )
        {
            
            header ('Content-Type: application/octet-stream'); 
            header ('Content-Disposition: attachment; filename='.$realname); 
            readfile($filename); 
        }
        else
        {
            echo 'File not readable...';
        }

        ob_end_flush();		
        ob_end_clean();		
    }
}

?>
